package com.mrjy.common.qtz;

import com.mrjy.common.entity.IntegralExchangedLog;
import com.mrjy.common.entity.RecordIncomeInfo;
import com.mrjy.common.entity.TransferInfo;
import com.mrjy.common.entity.UserBalancePayment;
import com.mrjy.integral.dao.IIntegralExchangedLogDao;
import com.mrjy.maintain.dao.IRecordIncomeInfoDao;
import com.mrjy.payment.bo.TransferPaymentBo;
import com.mrjy.payment.dao.ITransferInfoDao;
import com.mrjy.payment.dao.IUserBalancePaymentDao;
import org.apache.log4j.Logger;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 语音提问零钱退款
 */
public class RecordRefundSettlementJob {
    public static Integer counter = 0;

    @Resource
    TransferPaymentBo transferPaymentBo;
    @Resource
    IRecordIncomeInfoDao recordIncomeInfoDao;
    @Resource
    ITransferInfoDao transferInfoDao;
    @Resource
    IIntegralExchangedLogDao integralExchangedLogDao;
    @Resource
    IUserBalancePaymentDao userBalancePaymentDao;

    private static Logger logger = Logger.getLogger(RecordRefundSettlementJob.class);

    /**
     * @param
     * @Description: 执行任务，语音提问零钱退款
     */
    public void doJob() {
        synchronized (RecordRefundSettlementJob.counter) {
            logger.debug("语音提问零钱退款");
            List<RecordIncomeInfo> recordIncomeInfos = recordIncomeInfoDao.queryUnDoneIncomeAll();
            for (RecordIncomeInfo recordIncomeInfo : recordIncomeInfos) {
                Integer userId = recordIncomeInfo.getUserId();
                TransferInfo transInfo = new TransferInfo();

                BigDecimal totalFee = recordIncomeInfo.getMoney().multiply(new BigDecimal(100));
                transInfo.setAmount(totalFee);
                transInfo.setOutRefundNo("recordRef_" + System.currentTimeMillis());
                transInfo.setPartnerTradeNo(recordIncomeInfo.getCode());
                transInfo.setPaymentDesc("语音提问零钱退款");
                transInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
                transInfo.setRefundAccount("REFUND_SOURCE_RECHARGE_FUNDS");
                UserBalancePayment userBalancePayment = userBalancePaymentDao.queryPaymentByCode(recordIncomeInfo.getCode());
                userBalancePayment.setStatusId(4);
                userBalancePaymentDao.updatePayment(userBalancePayment);


                Map<String, Object> returnMap = (Map<String, Object>) transferPaymentBo.weChatRefundPay(transInfo);
                transInfo.setCompleteTime(new Timestamp(System.currentTimeMillis()));
                transInfo.setDisabledFlag(0 + "");
                if (returnMap.get("err_code").equals("200")) {
                    userBalancePayment.setRefundCode(returnMap.get("out_refund_no") + "");
                    userBalancePayment.setStatusId(3);
                    userBalancePaymentDao.updatePayment(userBalancePayment);

                    transInfo.setTransferStatus(1 + "");
                    recordIncomeInfo.setSettleStatus(3);//退款
                    int update = recordIncomeInfoDao.updateSettleStatusById(recordIncomeInfo);

                    if (recordIncomeInfo.getVoucherId() != null && update > 0) {
                        IntegralExchangedLog integralExchangedLog = integralExchangedLogDao.queryById(recordIncomeInfo.getVoucherId());
                        integralExchangedLog.setIsUse(0);
                        int update_log = integralExchangedLogDao.updateById(integralExchangedLog);
                        logger.debug("用户：" + userId + "代金券退款成功;返回状态为：" + update_log);
                    }
                } else {
                    transInfo.setTransferStatus(0 + "");
                    logger.warn("userid:" + userId + " 退款失败");
                }
                transInfo.setPaymentId(userBalancePayment.getId());
                transInfo.setErrCode(returnMap.get("err_code") + "");
                transInfo.setErrCodeDes(returnMap.get("err_code_des") + "");
                transInfo.setPartnerTradeNo(userBalancePayment.getCode().replace("_", ""));
                transInfo.setAmount(recordIncomeInfo.getMoney());
                int insert = transferInfoDao.insert(transInfo);
                logger.debug(returnMap);
            }
        }
    }
}